package com.andy.nature.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import com.andy.nature.databinding.ArticleItemBinding
import com.andy.nature.model.TestBean

/**
 * Author:Andy
 * Date:2023/3/20 19:25
 * Description:ArticleAdapter
 */
class TestAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private var mData: MutableList<TestBean> = mutableListOf()

    fun setData(data: MutableList<TestBean>) {
        mData.clear()
        mData.addAll(data)
        notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ArticleViewHolder(ArticleItemBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (mData.size > position) {
            val item = mData[position]
            (holder as ArticleViewHolder).bindData(item)
        }
    }

    internal class ArticleViewHolder(private var binding: ViewBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bindData(data: TestBean) {
            (binding as ArticleItemBinding).run {
                tvName.text = data.name
            }
        }
    }
}